package com.domain.controller;

import com.domain.common.Comments;
import com.domain.common.Const;
import com.domain.common.Result;
import com.domain.pojo.*;
import com.domain.service.*;
import com.domain.utils.NotNullUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

/**
 * 用户操作控制器
 */
@Controller
@RequestMapping("/users")
public class UsersController {

    @Autowired
    private UsersService usersService;
    @Autowired
    private SmsCodeService smsCodeService;
    @Autowired
    private PublishService publishService;
    @Autowired
    private UsersHelpService usersHelpService;

    @Autowired
    private PublishAcceptanceService publishAcceptanceService;

    @Autowired
    private UsersWalletService usersWalletService;

    @Autowired
    private FeedBackService feedBackService;


    /**
     * 修改个人信息
     *
     * @param userId        用户id
     * @param nickName      昵称
     * @param image         照片墙
     * @param name          真实姓名
     * @param gender        用户性别
     * @param birthday      用户生日
     * @param city          所在城市
     * @param industry      行业标签
     * @param personalLabel 个人标签
     * @param headImage     用户头像
     * @param request
     * @return
     */
    @RequestMapping(value = "/editUserInfo", method = RequestMethod.POST)
    @ResponseBody
    public Result editUserInfo(
            String userToken,
            String userId,
            String nickName,
            String image,
            String name,
            String gender,
            String birthday,
            String city,
            String industry,
            String personalLabel,
            String headImage,
            HttpServletRequest request) {
        try {
            //判断用户是否登录
            Users users = (Users) request.getSession().getAttribute(Const.CURRENT_USER);
            if (!NotNullUtils.isNotNullObject(users) || !users.getSpareOne().equals(userToken)) {
                return Result.publicResult(Comments.NEED_LOGIN, "请登录");
            }
            return usersService.editUserInfo(userId, nickName, image, name, gender, birthday, city, industry, personalLabel, headImage, request);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.fail("系统错误");
        }
    }

    /**
     * 修改手机号
     *
     * @param userId      用户id
     * @param newPhoneNum 新电话号码
     * @param smsCode     验证码
     * @param request
     * @return
     */
    @RequestMapping(value = "/changePhoneNum", method = RequestMethod.POST)
    @ResponseBody
    public Result changePhoneNum(String userToken, String userId, String newPhoneNum, String smsCode, HttpServletRequest request) {
        try {
            //判断用户是否登录
            Users users = (Users) request.getSession().getAttribute(Const.CURRENT_USER);
            if (!NotNullUtils.isNotNullObject(users) || !users.getSpareOne().equals(userToken)) {
                return Result.publicResult(Comments.NEED_LOGIN, "请登录");
            }

            //验证验证码码得时效性（五分钟）
            SmsCode sc = smsCodeService.checkSmsCodeTime(smsCode, newPhoneNum);
            if (!NotNullUtils.isNotNullObject(sc)) {
                return Result.publicResult(Comments.SMSCODE_FAIL, "验证码错误");
            } else {
                Long s = (System.currentTimeMillis() - sc.getCreatTime().getTime()) / (1000 * 60);
                if (s >= 5) {
                    //过期删除验证码
                    smsCodeService.delect(sc.getId());
                    return Result.publicResult(Comments.SMSCODE_CHECK, "验证码已过期！");
                }
            }
            return usersService.changePhoneNum(userId, newPhoneNum);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.fail("系统错误");
        }
    }

    /**
     * 我的
     *
     * @param userToken
     * @param userId
     * @param request
     * @return
     */
    @RequestMapping(value = "/getUsersPageInfo", method = RequestMethod.POST)
    @ResponseBody
    public Result getUsersPageInfo(String userToken, String userId, HttpServletRequest request) {
        try {
            //判断用户是否登录
            Users users = (Users) request.getSession().getAttribute(Const.CURRENT_USER);
            if (!NotNullUtils.isNotNullObject(users) || !users.getSpareOne().equals(userToken)) {
                return Result.publicResult(Comments.NEED_LOGIN, "请登录");
            }
            return usersService.getUsersPageInfo(userId);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.fail("系统错误");
        }
    }

    /**
     * 我的发布列表
     *
     * @param userToken 用户token
     * @param userId    用户id
     * @param pageNum   页号
     * @param pageSize  每页显示得条数
     * @param request
     * @return
     */
    @RequestMapping(value = "/getUsersPublishList", method = RequestMethod.POST)
    @ResponseBody
    public Result getUsersPublishList(String userToken, String userId, String pageNum, String
            pageSize, HttpServletRequest request) {
        try {
            //判断用户是否登录
            Users users = (Users) request.getSession().getAttribute(Const.CURRENT_USER);
            if (!NotNullUtils.isNotNullObject(users) || !users.getSpareOne().equals(userToken)) {
                return Result.publicResult(Comments.NEED_LOGIN, "请登录");
            }
            return publishService.getUsersPublish(userId, pageNum, pageSize);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.fail("系统错误!");
        }
    }

    /**
     * 发布详情
     *
     * @param userToken 用户token
     * @param userId    用户id
     * @param publishId 发布id
     * @param request
     * @return
     */
    @RequestMapping(value = "/getUserPublishDetail", method = RequestMethod.POST)
    @ResponseBody
    public Result getUserPublishDetail(String userToken, String userId, String publishId, HttpServletRequest
            request) {
        try {
            //判断用户是否登录
            Users users = (Users) request.getSession().getAttribute(Const.CURRENT_USER);
            if (!NotNullUtils.isNotNullObject(users) || !users.getSpareOne().equals(userToken)) {
                return Result.publicResult(Comments.NEED_LOGIN, "请登录");
            }
            return publishService.getUserPublishDetail(userId, publishId);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.fail("系统错误");
        }
    }

    /**
     * 编辑我的发布
     *
     * @param userToken    用户token
     * @param userId       用户id
     * @param publishId    发布id
     * @param title        发布title
     * @param content      发布类容
     * @param images       发布照片
     * @param filterUserId 过滤得好友
     * @param longitude    发布位置经度
     * @param latitude     发布位置纬度
     * @param cityName     发布城市名称
     * @param itIsPaid     发布是否有偿
     * @param typeId       发布类型id
     * @return
     */
    @RequestMapping(value = "/editUsersPublish", method = RequestMethod.POST)
    @ResponseBody
    public Result editUsersPublish(String userToken,
                                   String userId,
                                   String publishId,
                                   String title,
                                   String content,
                                   String images,
                                   String filterUserId,
                                   String longitude,
                                   String latitude,
                                   String cityName,
                                   String itIsPaid,
                                   String typeId,
                                   HttpServletRequest request) {

        try {
            //判断用户是否登录
            Users users = (Users) request.getSession().getAttribute(Const.CURRENT_USER);
            if (!NotNullUtils.isNotNullObject(users) || !users.getSpareOne().equals(userToken)) {
                return Result.publicResult(Comments.NEED_LOGIN, "请登录");
            }
            return usersService.editUsersPublish(userToken, userId, publishId, title, content, images, filterUserId, longitude, latitude, cityName, itIsPaid, typeId);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.fail("系统错误");
        }
    }


    /**
     * 删除我的发布
     *
     * @param userToken 用户token
     * @param
     * @param publishId 发布id
     * @param request
     * @return
     */
   /* @RequestMapping(value = "/deleteUsersPublish", method = RequestMethod.POST)
    @ResponseBody
    public Result deleteUsersPublish(String userToken, String publishId, HttpServletRequest request) {
        try {
            //判断用户是否登录
            Users users = (Users) request.getSession().getAttribute(Const.CURRENT_USER);
            if (!NotNullUtils.isNotNullObject(users) || !users.getSpareOne().equals(userToken)) {
                return Result.publicResult(Comments.NEED_LOGIN, "请登录");
            }
            return publishService.deleteUsersPublish(publishId);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.fail("系统错误");
        }
    }*/

    /**
     * 获取我的帮转列表
     *
     * @param userToken
     * @param userId
     * @param pageNum
     * @param pageSize
     * @param request
     * @return
     */
    @RequestMapping(value = "/getUsersPublishHelpList", method = RequestMethod.POST)
    @ResponseBody
    public Result getUsersPublishHelpList(String userToken, String userId, String pageNum, String
            pageSize, HttpServletRequest request) {
        try {
            //判断用户是否登录
            Users users = (Users) request.getSession().getAttribute(Const.CURRENT_USER);
            if (!NotNullUtils.isNotNullObject(users) || !users.getSpareOne().equals(userToken)) {
                return Result.publicResult(Comments.NEED_LOGIN, "请登录");
            }
            return usersHelpService.getUsersPublishHelpList(userId, pageNum, pageSize);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.fail("操作失败");
        }
    }

    /**
     * 删除我的帮转
     *
     * @param userToken 用户token
     * @param helpId    帮转id
     * @param request
     * @return
     */
    @RequestMapping(value = "/deleteHelpPublish", method = RequestMethod.POST)
    @ResponseBody
    public Result deleteHelpPublish(String userToken, String helpId, HttpServletRequest request) {
        //判断用户是否登录
        try {
            Users users = (Users) request.getSession().getAttribute(Const.CURRENT_USER);
            if (!NotNullUtils.isNotNullObject(users) || !users.getSpareOne().equals(userToken)) {
                return Result.publicResult(Comments.NEED_LOGIN, "请登录");
            }
            return usersHelpService.deleteHelpPublish(helpId);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.fail("操作失败");
        }
    }


    /**
     * 获取我的帮忙列表
     *
     * @param userToken
     * @param userId
     * @param pageNum
     * @param pageSize
     * @param request
     * @return
     */
    @RequestMapping(value = "/getUsersPublishAcceptanceList", method = RequestMethod.POST)
    @ResponseBody
    public Result getUsersPublishAcceptanceList(String userToken, String userId, String pageNum, String
            pageSize, HttpServletRequest request) {
        try {
            Users users = (Users) request.getSession().getAttribute(Const.CURRENT_USER);
            if (!NotNullUtils.isNotNullObject(users) || !users.getSpareOne().equals(userToken)) {
                return Result.publicResult(Comments.NEED_LOGIN, "请登录");
            }
            return publishAcceptanceService.getUsersPublishAcceptanceList(userId, pageNum, pageSize);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.fail("操作失败");
        }
    }

    /**
     * 获取我的钱包
     *
     * @param userToken
     * @param userId
     * @param request
     * @return
     */
    @RequestMapping(value = "/getUsersWallet", method = RequestMethod.POST)
    @ResponseBody
    public Result getUsersWallet(String userToken, String userId, HttpServletRequest request) {
        try {
            Users users = (Users) request.getSession().getAttribute(Const.CURRENT_USER);
            if (!NotNullUtils.isNotNullObject(users) || !users.getSpareOne().equals(userToken)) {
                return Result.publicResult(Comments.NEED_LOGIN, "请登录");
            }
            return usersWalletService.getUsersWallet(userId);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.fail("操作失败");
        }
    }


    /**
     * 绑定支付宝
     *
     * @param userToken    用户token
     * @param usersWalleId 用户钱包id
     * @param alipayNum    支付宝账号
     * @param request
     * @return
     */
    @RequestMapping(value = "/bindAlipay", method = RequestMethod.POST)
    @ResponseBody
    public Result bindAlipay(String userToken, String usersWalleId, String alipayNum, HttpServletRequest request) {
        try {
            Users users = (Users) request.getSession().getAttribute(Const.CURRENT_USER);
            if (!NotNullUtils.isNotNullObject(users) || !users.getSpareOne().equals(userToken)) {
                return Result.publicResult(Comments.NEED_LOGIN, "请登录");
            }
            return usersWalletService.bindAlipay(usersWalleId, alipayNum);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.fail("操作失败");
        }
    }

    /**
     * 获取用户账单列表
     *
     * @param userToken 用户token
     * @param userId    用户id
     * @param startTime 开始时间
     * @param endTime   结束时间
     * @param pageNum   页码
     * @param pagaSize
     * @param request
     * @return
     */
    @RequestMapping(value = "/getUserBillDetail", method = RequestMethod.POST)
    @ResponseBody
    public Result getUserBillDetail(String userToken, String userId, String startTime, String endTime, String pageNum, String pagaSize, HttpServletRequest request) {
        try {
            Users users = (Users) request.getSession().getAttribute(Const.CURRENT_USER);
            if (!NotNullUtils.isNotNullObject(users) || !users.getSpareOne().equals(userToken)) {
                return Result.publicResult(Comments.NEED_LOGIN, "请登录");
            }
            return usersService.getUserBillDetail(userId, startTime, endTime, pageNum, pagaSize);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.fail("系统错误");
        }
    }

    /**
     * 屏蔽好友
     *
     * @param userToken
     * @param userId
     * @param fillterUserIds
     * @param fillterType
     * @param request
     * @return
     */
    @RequestMapping(value = "/fillterUsers", method = RequestMethod.POST)
    @ResponseBody
    public Result fillterUsers(String userToken, String userId, String fillterUserIds, String fillterType, HttpServletRequest request) {
        try {
            Users users = (Users) request.getSession().getAttribute(Const.CURRENT_USER);
            if (!NotNullUtils.isNotNullObject(users) || !users.getSpareOne().equals(userToken)) {
                return Result.publicResult(Comments.NEED_LOGIN, "请登录");
            }
            return usersService.fillterUsers(userId, fillterUserIds, fillterType);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.fail("系统错误");
        }
    }


    /**
     * 用户反馈
     *
     * @param userToken 用户token
     * @param userId    用户id
     * @param content   反馈内容
     * @param request
     * @return
     */
    @RequestMapping(value = "/addFeedBack", method = RequestMethod.POST)
    @ResponseBody
    public Result addFeedBack(String userToken, String userId, String content, HttpServletRequest request) {
        try {
            Users users = (Users) request.getSession().getAttribute(Const.CURRENT_USER);
            if (!NotNullUtils.isNotNullObject(users) || !users.getSpareOne().equals(userToken)) {
                return Result.publicResult(Comments.NEED_LOGIN, "请登录");
            }
            return feedBackService.addFeedBack(userId, content);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.fail("系统错误");
        }
    }


}
